package com.ruoyi.web.controller.news;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.news.NewsProtectWork;
import com.ruoyi.system.domain.news.dto.NewsProtectWorkDto;
import com.ruoyi.system.domain.news.query.NewsProtectWorkQuery;
import com.ruoyi.system.domain.news.vo.NewsProtectWorkVo;
import com.ruoyi.system.service.news.NewsProtectWorkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/news/newsProtectWork")
@Api(value = "newsProtectWorkController", tags  = "等级保护工作")
public class NewsProtectWorkController extends BaseController {

	@Resource
	private NewsProtectWorkService newsProtectWorkService;

    @ApiOperation(value = "新增/修改等级保护工作信息", notes = "新增/修改等级保护工作信息")
	@PostMapping("/submit")
	public R submit(@RequestBody NewsProtectWorkDto NewsProtectWorkDto) {
		LoginUser loginUser = SecurityUtils.getLoginUser();
		NewsProtectWork NewsProtectWork = new NewsProtectWork();
		BeanUtils.copyProperties(NewsProtectWorkDto,NewsProtectWork);
		if (NewsProtectWork.getId()==null) {
			NewsProtectWork.setCreateBy(loginUser.getUsername());
			NewsProtectWork.setDeptId(loginUser.getDeptId());
			NewsProtectWork.setCreateTime(new Date());
			NewsProtectWork.setUpdateBy(loginUser.getUsername());
			NewsProtectWork.setUpdateTime(new Date());
		} else {
			NewsProtectWork.setUpdateBy(loginUser.getUsername());
			NewsProtectWork.setUpdateTime(new Date());
		}
		boolean result = newsProtectWorkService.saveOrUpdate(NewsProtectWork);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "删除等级保护工作信息", notes = "删除等级保护工作信息")
	@DeleteMapping("/delete")
	public R delete(@RequestParam(value = "id") Long id) {
		boolean result = newsProtectWorkService.removeById(id);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "获取单个等级保护工作信息", notes = "获取单个等级保护工作信息")
	@GetMapping("/getById")
	public R<NewsProtectWorkVo> getById(@RequestParam(value = "id") Long id) {
		NewsProtectWorkQuery newsProtectWorkQuery = new NewsProtectWorkQuery();
		newsProtectWorkQuery.setId(id);
		List<NewsProtectWorkVo> newsProtectWorkList = newsProtectWorkService.getNewsProtectWorkList(newsProtectWorkQuery);
		if (newsProtectWorkList!=null&&newsProtectWorkList.size()>0) {
			return R.ok(newsProtectWorkList.get(0));
		} else {
			return R.fail("数据不存在");
		}
	}

	@ApiOperation(value = "获取等级保护工作信息列表")
	@PostMapping(value = "/list")
	public R<PageInfo<NewsProtectWorkVo>> list(@RequestBody NewsProtectWorkQuery newsProtectWorkQuery) {
		List<NewsProtectWorkVo> newsProtectWorkList = newsProtectWorkService.getNewsProtectWorkList(newsProtectWorkQuery);
		return R.ok(new PageInfo<>(newsProtectWorkList));
	}
}
